package com.tm.mholmu.application;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import jxl.Sheet;
import jxl.Workbook;

import android.R.integer;

import com.tm.mholmu.db.DBUtils;
import com.tm.mholmu.db.Outfit;
import com.tm.mholmu.db.Skill;

public class Util {
 
	/**
	 * 读取excel表格数据
	 */
	public  static List<Outfit> intidata() {
		List<Outfit> lo = new ArrayList<Outfit>();
		try {
			/**
			 * 后续考虑问题,比如Excel里面的图片以及其他数据类型的读取
			 **/
			// InputStream is = new FileInputStream("mnt/sdcard/test.xls");
			InputStream is = MHOApplication.getInstance().getAssets()
					.open("12.xls");
			// is=context.getClass().getClassLoader().getResourceAsStream("12.xls");这是as的读取方式

			// Workbook book = Workbook.getWorkbook(new
			// File("mnt/sdcard/test.xls"));
			Workbook book = Workbook.getWorkbook(is);// 得到一个工作簿

			int num = book.getNumberOfSheets();
			StringBuffer s = new StringBuffer();

			s.append("这个工作簿一共有" + num + "张表\n");
			// 2yuan 3tou 4name 5j1 6j2 7j3 8j4 9j5 10n1 11n2 12n3 13n4 14n5
			// 15kong 16quanbu-jineng
			Sheet sheet = book.getSheet(2);// 得到一个工作表
			int Rows = sheet.getRows();// 行数
			int Cols = sheet.getColumns();// 列数
			s.append("这个表的名称是" + sheet.getName() + "\n");
			s.append("总行数 " + Rows + "\n");
			s.append("总列数 " + Cols + "\n");
			Outfit o;
			for (int i = 1; i < Rows; ++i) {
				o = new Outfit(sheet.getRow(i)[4].getContents(),
						sheet.getRow(i)[2].getContents(),
						sheet.getRow(i)[3].getContents(),
						Integer.valueOf(sheet.getRow(i)[15].getContents()),
						sheet.getRow(i)[16].getContents()+"|");
				o.N_1=DBUtils.getInstance().getDBService().getSkill_Int(sheet.getRow(i)[5].getContents());
				o.N_2=DBUtils.getInstance().getDBService().getSkill_Int(sheet.getRow(i)[6].getContents());
				o.N_3=DBUtils.getInstance().getDBService().getSkill_Int(sheet.getRow(i)[7].getContents());
				o.N_4=DBUtils.getInstance().getDBService().getSkill_Int(sheet.getRow(i)[8].getContents());
				o.N_5=DBUtils.getInstance().getDBService().getSkill_Int(sheet.getRow(i)[9].getContents());
				o.N_6=DBUtils.getInstance().getDBService().getSkill_Int("");
				
				o.Z_1=Integer.valueOf(sheet.getRow(i)[10].getContents().equals("")?"0":sheet.getRow(i)[10].getContents());
				o.Z_2=Integer.valueOf(sheet.getRow(i)[11].getContents().equals("")?"0":sheet.getRow(i)[11].getContents());
				o.Z_3=Integer.valueOf(sheet.getRow(i)[12].getContents().equals("")?"0":sheet.getRow(i)[12].getContents());
				o.Z_4=Integer.valueOf(sheet.getRow(i)[13].getContents().equals("")?"0":sheet.getRow(i)[13].getContents());
				o.Z_5=Integer.valueOf(sheet.getRow(i)[14].getContents().equals("")?"0":sheet.getRow(i)[14].getContents());
				o.Z_6=0;
				
				// getCell(Col,Row)获得单元格的值
				// s.append("contents:" + sheet.getCell(i, j).getContents() +
				// "\n");
				// o.N_1=sheet.getColumn(i)[16].getContents() ;
				lo.add(o);
				// sheet.getColumn(i)[4],sheet.getColumn(i)[2],sheet.getColumn(i)[3],
				// sheet.getColumn(i)[15],sheet.getColumn(i)[16],

			}
			book.close();
		} catch (Exception e) {
			System.out.println(e);
		}
		return lo;

	}

	public static List<Skill>  intiskill() {

		List<Skill> lo = new ArrayList<Skill>();
		try {
			/**
			 * 后续考虑问题,比如Excel里面的图片以及其他数据类型的读取
			 **/
			// InputStream is = new FileInputStream("mnt/sdcard/test.xls");
			InputStream is = MHOApplication.getInstance().getAssets()
					.open("12.xls");
			// is=context.getClass().getClassLoader().getResourceAsStream("12.xls");这是as的读取方式

			// Workbook book = Workbook.getWorkbook(new
			// File("mnt/sdcard/test.xls"));
			Workbook book = Workbook.getWorkbook(is);// 得到一个工作簿

			int num = book.getNumberOfSheets();
			StringBuffer s = new StringBuffer();

			s.append("这个工作簿一共有" + num + "张表\n");
			// 2yuan 3tou 4name 5j1 6j2 7j3 8j4 9j5 10n1 11n2 12n3 13n4 14n5
			// 15kong 16quanbu-jineng
			Sheet sheet = book.getSheet(3);// 得到一个工作表
			int Rows = sheet.getRows();// 行数
			int Cols = sheet.getColumns();// 列数
			s.append("这个表的名称是" + sheet.getName() + "\n");
			s.append("总行数 " + Rows + "\n");
			s.append("总列数 " + Cols + "\n");
			Skill o;
			for (int i = 1; i < Rows; ++i) {
			/*	System.err.println(Rows);
			
				System.err.println(""+sheet.getRow(i)[3].getContents()+"--"+
						sheet.getRow(i)[4].getContents()+"--"+
						sheet.getRow(i)[1].getContents()+"--"+
						sheet.getRow(i)[2].getContents());*/
				o = new Skill(sheet.getRow(i)[3].getContents(),
						sheet.getRow(i)[4].getContents(),
						Integer.valueOf(sheet.getRow(i)[1].getContents()),
						Integer.valueOf(sheet.getRow(i)[2].getContents()));

				lo.add(o);
				// sheet.getColumn(i)[4],sheet.getColumn(i)[2],sheet.getColumn(i)[3],
				// sheet.getColumn(i)[15],sheet.getColumn(i)[16],

			}
			book.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lo;

	}

	public static int get() {

		return 0;
	}

	public static int[] StringToInt(String s) {
	//|65|66||55|56||55|
		String[] ss;
		ss=s.split("\\|");
		List<String> s1 = new ArrayList<String>();
		for (int i = 0; i < ss.length; i++) {
			if (!ss[i].equals("")&&!s1.contains(ss[i])) {
				s1.add(ss[i]);
			}
		}
		int[] i = new int[s1.size()] ;
		for (int j = 0; j < s1.size(); j++) {
				i[j]=Integer.valueOf(s1.get(j));
		}
		return i;
	}

	public static String IntToString(int[] i) {
		if (i.length != 81) {
			return null;
		} else {
			StringBuffer sb = new StringBuffer();
			for (int t = 0; t < i.length; t++) {
				sb.append(String.valueOf(i[t]));
				if (t != i.length) {
					sb.append("-");
				}
			}
			return sb.toString();

		}

	}
}
